package com.xmall.product.controller;

import com.xmall.aspect.OperateRecord;
import com.xmall.aspect.RepeatSubmit;
import com.xmall.define.base.Result;
import com.xmall.define.dict.OperateTypeEnum;
import com.xmall.product.define.dto.saleAttr.SaleAttrDto;
import com.xmall.product.define.request.attr.SaleAttrQueryParams;
import com.xmall.product.define.request.attr.SaleAttrUpdateRequest;
import com.xmall.product.service.ICategoryService;
import com.xmall.product.service.ISaleAttrService;
import com.xmall.validate.group.AddGroup;
import com.xmall.validate.group.UpdateGroup;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 属性
 */
@RestController
@RequestMapping("/service/product/saleAttr")
public class SaleAttrController {

    @Resource
    private ISaleAttrService saleAttrService;

    @Resource
    private ICategoryService categoryService;

    /**
     * 属性列表
     */
    @GetMapping("/list/{categoryId}")
    public Result<List<SaleAttrDto>> list(@PathVariable("categoryId") Long categoryId){
        List<SaleAttrDto> list = saleAttrService.listWithCascade(new SaleAttrQueryParams().setCategoryIds(Arrays.asList(categoryId)));
        return null != list ? Result.success(list) : Result.error();
    }

    /**
     * 属性列表
     */
    @GetMapping("/list/{categoryId}/withParent")
    public Result<List<SaleAttrDto>> listWithParent(@PathVariable("categoryId") Long categoryId){
        List<Long> categoryIds = categoryService.hierarchyIdList(categoryId);
        List<SaleAttrDto> list = saleAttrService.listWithCascade(new SaleAttrQueryParams().setCategoryIds(categoryIds));
        return null != list ? Result.success(list) : Result.error();
    }

    /**
     * 详情
     */
    @GetMapping("/{id}")
    public Result<SaleAttrDto> info(@PathVariable("id") Long id){
        SaleAttrDto attrDto = saleAttrService.infoWithCascade(id);
        return null != attrDto ? Result.success(attrDto) : Result.error();
    }

    /**
     * 新增
     */
    @OperateRecord(type = OperateTypeEnum.ADD)
    @RepeatSubmit
    @PreAuthorize("hasAuthority('product:saleAttr:save')")
    @PostMapping("/save")
    public Result save(@Validated(value = AddGroup.class) @RequestBody SaleAttrUpdateRequest request){
        saleAttrService.saveEntity(request);
        return Result.success();
    }

    /**
     * 编辑
     */
    @OperateRecord(type = OperateTypeEnum.UPDATE)
    @PreAuthorize("hasAuthority('product:saleAttr:update')")
    @PostMapping("/update")
    public Result update(@Validated(value = UpdateGroup.class) @RequestBody SaleAttrUpdateRequest request){
        saleAttrService.updateEntity(request);
        return Result.success();
    }

    /**
     * 删除
     */
    @OperateRecord(type = OperateTypeEnum.DELETE)
    @PreAuthorize("hasAuthority('product:saleAttr:delete')")
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id){
        saleAttrService.deleteEntity(id);
        return Result.success();
    }

    /**
     * 交换排序值
     */
    @OperateRecord(type = OperateTypeEnum.UPDATE)
    @PostMapping("/swapSort/{prevAttrId}/{nextAttrId}")
    public Result swapSort(@PathVariable("prevAttrId") Long prevAttrId,
                           @PathVariable("nextAttrId") Long nextAttrId){
        saleAttrService.swapSort(prevAttrId, nextAttrId);
        return Result.success();
    }
}
